package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzq.domain.dto.SmileShootAddressDTO;
import com.zzq.domain.entity.SmileShootAddress;
import com.zzq.domain.entity.SmileShootAddressSample;
import com.zzq.domain.search.ShootAddressSearchParamet;
import com.zzq.exception.NormalException;
import com.zzq.mapper.SmileShootAddressMapper;
import com.zzq.service.SmileShootAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzq.service.SmileShootAddressSampleService;
import com.zzq.util.CommonUtils;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 谢文宗
 * @since 2023-08-22
 */
@Service
public class SmileShootAddressServiceImpl extends ServiceImpl<SmileShootAddressMapper, SmileShootAddress> implements SmileShootAddressService {

    @Autowired
    SmileShootAddressMapper smileShootAddressMapper;

    @Autowired
    SmileShootAddressSampleService smileShootAddressSampleService;
    @Override
    public  List<SmileShootAddressDTO> getShootAddressDTO(ShootAddressSearchParamet searchParamet) {
        List<SmileShootAddressDTO> shootAddressApplyDTOs = smileShootAddressMapper.getShootAddressDTO(searchParamet);
        if (searchParamet.getWithSample()) {
            for (SmileShootAddressDTO shootAddressApplyDTO : shootAddressApplyDTOs) {
                Long id = shootAddressApplyDTO.getId();
                LambdaQueryWrapper<SmileShootAddressSample> queryWrapper=new LambdaQueryWrapper();
                queryWrapper.eq(SmileShootAddressSample::getShootAddressId,id);
                List<SmileShootAddressSample> list = smileShootAddressSampleService.list(queryWrapper);
                if (!ObjectUtils.isEmpty(list)) {
                    shootAddressApplyDTO.setSmileShootAddressSamples(list);
                }
            }
        }
        return shootAddressApplyDTOs;
    }

    @Override
    public SmileShootAddressDTO getShootAddressDTObyId(Long id) {
        ShootAddressSearchParamet searchParamet =new ShootAddressSearchParamet();
        searchParamet.setShootAddressId(id);
        List<SmileShootAddressDTO> shootAddressApplyDTOs = smileShootAddressMapper.getShootAddressDTO(searchParamet);
        if (shootAddressApplyDTOs.size()>1){
            throw new NormalException("查询异常");
        }
        return shootAddressApplyDTOs.get(0);
    }

    @Override
    public Boolean getScopeTarget(BigDecimal shootLongitude, BigDecimal shootLatitude,Long radius) {
        List<SmileShootAddress> smileShootAddresselist = smileShootAddressMapper.selectList(null);
        for (SmileShootAddress smileShootAddress : smileShootAddresselist) {
            BigDecimal targetLatitude = smileShootAddress.getShootLatitude();
            BigDecimal targetLongitude = smileShootAddress.getShootLongitude();
            boolean pointInRadius = CommonUtils.isPointInRadius(shootLongitude, shootLatitude, targetLongitude, targetLatitude, radius);
            if (pointInRadius){
                return pointInRadius;
            }
        }
        return false;
    }
}
